package com.imast.android.bys.util;

import java.util.HashMap;

import com.imast.android.bys.io.model.Usuario;
import com.imast.android.bys.ui.SplashActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SessionManager {

	public static final int BYS_LOGIN = 0;
	public static final int FB_LOGIN = 1;
	public static final int GPLUS_LOGIN = 2;
	
	// Shared Preferences
	SharedPreferences pref;

	// Editor for Shared preferences
	Editor editor;

	// Context
	Context _context;

	// Shared pref mode
	int PRIVATE_MODE = 0;

	// Sharedpref file name
	private static final String PREF_NAME = "BySPref";

	// All Shared Preferences Keys
	private static final String IS_LOGIN = "IsLoggedIn";
	
	// User (make variable public to access from outside)
	public static final String KEY_USER = "user";
	
	// User name (make variable public to access from outside)
	public static final String KEY_NAME = "name";

	// Email address (make variable public to access from outside)
	public static final String KEY_EMAIL = "email";

	// Tipo Login (make variable public to access from outside)
	public static final String KEY_TIPO_LOGIN = "tipoLogin";

	// Facebook id (make variable public to access from outside)
	public static final String KEY_GPLUS_ID = "gplus_id";

	// Google+ id (make variable public to access from outside)
	public static final String KEY_FB_ID = "fb_id";

	// Constructor
	public SessionManager(Context context) {
		this._context = context;
		pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
		editor = pref.edit();
	}

	// Create login session

	public void createLoginSession(String user, String name, String email, int tipoLogin,
			String fbId, String gplusId) {
		editor.putString(KEY_USER, user);
		editor.putBoolean(IS_LOGIN, true);
		editor.putString(KEY_NAME, name);
		editor.putString(KEY_EMAIL, email);
		editor.putInt(KEY_TIPO_LOGIN, tipoLogin);

		switch (tipoLogin) {
		case 1:
			editor.putString(KEY_FB_ID, fbId);
			break;
		case 2:
			editor.putString(KEY_GPLUS_ID, gplusId);
			break;
		}

		editor.commit();
	}
	
	//Get stored session data
	
    public Usuario getUserDetails(){
        Usuario usuario = new Usuario();
        
        usuario.setUser(pref.getString(KEY_USER, null));
        usuario.setName(pref.getString(KEY_NAME, null));
        usuario.setTipoLogin(pref.getInt(KEY_TIPO_LOGIN, -1));
        usuario.setEmail(pref.getString(KEY_EMAIL, null));
        usuario.setFbId(pref.getString(KEY_FB_ID, null));
        usuario.setGpplusId(pref.getString(KEY_GPLUS_ID, null));
        
        return usuario;
    }
    
    /**
     * Check login method wil check user login status
     * If false it will redirect user to login page
     * Else won't do anything
     * */
    public void checkLogin(){
        // Check login status
        if(!this.isLoggedIn()){
            // user is not logged in redirect him to Login Activity
            Intent i = new Intent(_context, SplashActivity.class);
            // Closing all the Activities
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             
            // Add new Flag to start new Activity
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             
            // Staring Login Activity
            _context.startActivity(i);
        }
         
    }
    
    /**
     * Clear session details
     * */
    public void logoutUser(){
        // Clearing all data from Shared Preferences
        editor.clear();
        editor.commit();
         
        // After logout redirect user to Loing Activity
        Intent i = new Intent(_context, SplashActivity.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         
        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         
        // Staring Login Activity
        _context.startActivity(i);
        
    }
    
 // Get Login State
    public boolean isLoggedIn(){
        return pref.getBoolean(IS_LOGIN, false);
    }
}
